package com.ugrow.ugrowcommunity.activity.announcement.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.ugrow.ugrowcommunity.R;
import com.ugrow.ugrowcommunity.activity.announcement.ADetailsActivity;
import com.ugrow.ugrowcommunity.activity.announcement.bean.AnnouncementBean;

import java.util.ArrayList;

public class AnnouncementAdapter extends RecyclerView.Adapter<AnnouncementAdapter.ViewHolder>{

    private ArrayList<AnnouncementBean> educationList;
    private Context context;

    public  AnnouncementAdapter(ArrayList arrayList,Context context){
       this.educationList = arrayList;
       this.context = context;
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {


        private  final TextView tvTitle,tvContent,tvAuthor;
        private  final ImageView imgACImg;
        private  final LinearLayout layoutOnclick;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_title);
            tvContent = itemView.findViewById(R.id.tv_content);
            tvAuthor = itemView.findViewById(R.id.tv_author);
            imgACImg = itemView.findViewById(R.id.img_acimg);
            layoutOnclick = itemView.findViewById(R.id.layout_onclick);
        }

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_announcement,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        AnnouncementBean announcementBean = educationList.get(position);
        holder.tvTitle.setText("# "+announcementBean.getTitle().substring(0,10));
        holder.tvContent.setText(announcementBean.getContent().substring(0,20));
        holder.tvAuthor.setText(announcementBean.getAuthor()+" "+announcementBean.getTime());

        Glide.with(context).load(announcementBean.getImgView()).into(holder.imgACImg);
        //跳转页面
        holder.layoutOnclick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ADetailsActivity.actionStart(context,announcementBean);
            }
        });
    }

    @Override
    public int getItemCount() {
        return educationList.size();
    }

}
